package com.example.reggie_take_out.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.reggie_take_out.commen.BaseContext;
import com.example.reggie_take_out.commen.CustomException;
import com.example.reggie_take_out.commen.R;
import com.example.reggie_take_out.entry.ShoppingCart;
import com.example.reggie_take_out.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 购物车添加数据
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        //首先获得当前用户id 给购物车设置
        Long id = BaseContext.getId();
        shoppingCart.setUserId(id);
        //然后通过判断前端选择的是dish 还是setmeal 来生成ShoppingCart对象
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        if(shoppingCart.getDishId() != null){
            //如果是dish对象
            queryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else{
            //如果是setmeal对象
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //判断ShoppingCart是否已经在购物车中 已在 +1 不在 为1
        ShoppingCart one = shoppingCartService.getOne(queryWrapper);
        if(one == null){
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);

        }else {
            one.setNumber(one.getNumber()+1);
            shoppingCartService.updateById(one);
            //统一返回数据 把one作为shoppingCart返回
            shoppingCart = one;

        }
        //返回R
        return R.success(shoppingCart);
    }

    /**
     * 查看购物车中的信息
     * @return
     */
    @GetMapping("/list")
    public  R<List<ShoppingCart>> list(){
        log.info("每个用户的购物车不同");
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId,BaseContext.getId());
        wrapper.orderByDesc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        return R.success(list);
    }

    /**
     *
     * @return
     */

    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){
            LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ShoppingCart::getUserId,BaseContext.getId());
            if(shoppingCart.getDishId() != null){
                //如果传过来的是菜品id
                wrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
            }else{
                //如果传过来的是套餐id
                wrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
            }
            ShoppingCart one = shoppingCartService.getOne(wrapper);
            if(one != null){
                if(one.getNumber() <= 0)
                {
                    throw new CustomException("该商品不存在");
                }
                one.setNumber(one.getNumber()-1);
                shoppingCartService.updateById(one);
                return R.success("");
            }else {
                throw new CustomException("该商品不存在");

            }

    }



    /**
     * 根据用户id删除购物车中所有的商品
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getId());
        shoppingCartService.remove(queryWrapper);
        return R.success("购物车已清楚成功");
    }

}
